#!/usr/bin/sh

modprobe vfd

if [ -e /var/etc/.firstboot ]; then
   rm /var/etc/.firstboot
   sleep 1
   echo "WAIT 3 min"
   touch /dev/.devfsd
    mkdir /dev/dbox
    mkdir /dev/input
    mknod /dev/dbox/vfd c 147 0
    mknod /dev/dbox/avs0 c 10 221
    mknod /dev/input/uinput c 10 223
    echo "WAIT 3 min" > /dev/dbox/vfd
fi

if [ -e /etc/init.d/mountvirtfs ]; then
   /etc/init.d/mountvirtfs start
fi

if [ -e /etc/init.d/portmap ]; then
   /etc/init.d/portmap start
fi

modprobe e2_proc

#network first
if [ -e /etc/init.d/udhcpc ]; then
   /etc/init.d/udhcpc start
fi

if [ -e /etc/init.d/vsftpd ]; then
   /etc/init.d/vsftpd start
fi
#/usr/sbin/inetd

if [ ! -e /var/etc/.firstboot ]; then

   #overclock, to speedup things
   #wartosci: "7683"=270MHz, "8963"=315MHz, "9475"=333MHz
   modprobe cpu_frequ
   echo "7683" > /proc/cpu_frequ/pll0_ndiv_mdiv

   echo "LOADING MODULES" > /dev/dbox/vfd
   echo "LOADING MODULES"   

   echo "init AVS"
   modprobe avs type=fake_avs #avs_none

   echo "init stmfb"
   modprobe stmcore-display-stx7109c3
   modprobe stmfb display0=1280x720-32@50:16m:pal:rgb:rgb

   echo "load a/v firmware"
   /usr/bin/ustslave /dev/st231-1 /boot/audio.elf
   /usr/bin/ustslave /dev/st231-0 /boot/video.elf

   echo "init embx"
   modprobe embxshell
   modprobe embxmailbox mailbox0=0xb9211000:136:set2 mailbox1=0xb9212000:0 ;
   modprobe embxshm mailbox0=shm:0:7:0x60000000:0:256:256:0:512:0xa4000000:0x10000000 ;
   modprobe mme_host transport0=shm

   echo "init player"
   modprobe mmelog
   modprobe mpeg2hw mmeName=MPEG2_TRANSFORMER1;
   modprobe ksound
   modprobe p2div64
   modprobe stm_v4l2
   modprobe pseudocard
   modprobe cx24116 useUnknown=1
   modprobe dummy_cic
   modprobe pti
   modprobe stmsysfs
   modprobe stm_monitor
   modprobe stmdvb
   
   modprobe player2
   modprobe sth264pp
   modprobe stmalloc
   modprobe platform
   modprobe silencegen
   [ -e /lib/modules/2.6.32.71_stm24_0217/extra/smartcard ] && modprobe smartcard
fi

showiframe /usr/share/bootlogo.mvi &
#stfbcontrol a 0

if [ ! -e /var/etc/.firstboot ]; then
   echo "enigma2" > /dev/dbox/vfd
   echo "LOADING e2"

if [ -e /etc/init.d/mountall ]; then
   /etc/init.d/mountall start
fi

   mkdir -p /var/run/lirc
   /usr/bin/lircd

   modprobe simu_button
   /usr/bin/evremote2 useLircdName &
  
   stfbcontrol a 255
   until false
   do
      echo "starting e2->"
      /usr/bin/enigma2
      rtv=$?
      echo "e2 ended <- RTV: " $rtv
      case "$rtv" in
		0) echo "0"
		   echo "E2 ERROR"
    		   init 6;;
		1) echo "1"
		   echo "SHUTDOWN"
		   echo "SHUTDOWN" > /dev/dbox/vfd
		   init 0;;
		2) echo "2"
		   echo "REBOOT"
		   init 6;;
		3) echo "3"
		   echo "E2 RESTART"
		   showiframe -p /usr/share/bootlogo.mvi &
		   echo "0" > /proc/progress;;
		*) echo "*"
		   echo "ERROR"
		   showiframe /usr/share/bootlogo.mvi &
		   echo "0" > /proc/progress
		   #exit 0
		   ;;
     esac
   done 
fi
